﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using KTXD.Data;

namespace UserKTXD.Scripts
{
    public partial class Thay_Doi_Mat_Khau : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["DangNhap"] == null)
                {
                    Response.Redirect("~/");
                }
            }
        }
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        protected void cmdSenMess_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies.Get("UserNameAndPassword");
            if (txtMatKhauCu.Text != DataRepository.ThanhVienProvider.GetByTenTruyCap(cookie.Values["UserName"].ToString()).MatKhau)
            {
                ScriptManager.RegisterStartupScript(upLienHe, this.GetType(), Constants.THATBAI, "alert('Mật khẩu cũ không đúng. Vui lòng thử lại. Xin cảm ơn!');", true);
                return;
            }
            if (txtMatKhauMoi.Text != txtMatKhauMoi2.Text)
            {
                ScriptManager.RegisterStartupScript(upLienHe, this.GetType(), Constants.THATBAI, "alert('Xác nhận mật khẩu không giống. Vui lòng thử lại. Xin cảm ơn!');", true);
                return;
            }
            conn.Open();
            string query = "UPDATE ThanhVien SET MatKhau = '" + txtMatKhauMoi.Text + "' where TenTruyCap = '" + cookie.Values["UserName"].ToString() + "'";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = query;
            if(sqlCommand.ExecuteNonQuery()==1)
            ScriptManager.RegisterStartupScript(upLienHe, this.GetType(), Constants.THANHCONG, "alert('Thay đổi mật khẩu thành công!');", true);
            conn.Close();
            
        }
    }
}